package org.apache.database;



import org.apache.database.dao.Condition;
import org.apache.database.helper.DeleteHelper;
import org.apache.database.helper.InsertHelper;
import org.apache.database.helper.QueryHelper;
import org.apache.database.helper.UpdateHelper;
import org.apache.database.infs.DeleteInterface;
import org.apache.database.infs.InsertInterface;
import org.apache.database.infs.QueryInterface;
import org.apache.database.infs.UpdateInterface;
import org.apache.database.pool.ConnectionPool;

import java.io.Serializable;
import java.util.List;

/**
 * 对外提供查询接口，重载层
 */
public class HTomcatPal implements QueryInterface, UpdateInterface, InsertInterface, DeleteInterface {
    private QueryHelper queryHelper =new QueryHelper();

    private UpdateHelper updateHelper =new UpdateHelper();

    private InsertHelper insertHelper =new InsertHelper();

    private DeleteHelper deleteHelper = new DeleteHelper();

    protected HTomcatPal(ConnectionPool pool) {
        deleteHelper.getSqlExecute().setConnectionPool(pool);
        insertHelper.getSqlExecute().setConnectionPool(pool);
        queryHelper.getSqlExecute().setConnectionPool(pool);
        updateHelper.getSqlExecute().setConnectionPool(pool);

    }

    @Override
    public <T> T findOneById(Class<T> tClass, Serializable value) {
        return queryHelper.findOneById(tClass,value);
    }

    @Override
    public <T> List<T> findAll(Class<T> tClass, int offset, int page) {
        return  queryHelper.findAll( tClass,offset,page);
    }

    @Override
    public <T> List<T> findAll(Class<T> tClass) {
        return findAll(tClass,-1,-1);
    }

    @Override
    public <T> List<T> findAll(Class<T> tClass, Condition condition) {
        if (condition==null){
            return findAll(tClass);
        }
        return queryHelper.findWithCondition(tClass,condition);
    }

    @Override
    public int updateById(Object o) {
        return updateHelper.updateById(o);
    }

    @Override
    public int update(Object o, Condition condition, String... field) {
        return updateHelper.update(o,condition,field);
    }

    @Override
    public int insert(Object o) {
        return insertHelper.insert(o);
    }
    @Override
    public int insert(List<?> objects) {
        for (int i = 0; i < objects.size(); i++) {
            insert(objects.get(i));
        }
        return 0;
    }


    @Override
    public int deleteByCondition(Class<?> tabClass, Condition condition) {
        return deleteHelper.deleteByCondition(tabClass,condition);
    }

    @Override
    public int getCount(Class<?> tClass) {
        return queryHelper.getCount(tClass);
    }

    @Override
    public int deleteIds(Class<?> tabClass, Serializable... ids) {
        return deleteHelper.deleteIds(tabClass,ids);
    }


}
